<?php

namespace App\Services\Huanxin;

class GroupServices extends BaseService
{
    /**
     * 获取App中所有的群组
     * $limit 每页条数 默认15条
     * $cursor 那一页  默认第一页
     * return false 或者true
     */
    public function getAllGroup($limit = 15, $cursor = 1)
    {
        $url = $this->url . 'chatgroups?limit=' . $limit . '&cursor=' . $cursor;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, "GET");
        return resultHuanxinInfo($data);
    }

    /**
     * 获取群组详情
     * $group_ids 群组ID
     * return false 或者true
     */
    public function getGroupDetails($group_ids)
    {
        if (empty($group_ids)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_ids;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 创建一个群组
     * $groupname 群组名称，此属性为必须的，最大长度为128字符
     * $desc 群组描述，此属性为必须的，最大长度为512字符
     * $public 是否是公开群，此属性为必须的
     * $allowinvites 是否允许群成员邀请别人加入此群。 true：允许群成员邀请人加入此群，false：只有群主或者管理员才可以往群里加人。注：如果是公开群（public为true），则不允许群成员邀请别人加入此群
     * $owner 群组的管理员，此属性为必须的
     * $membersonly 用户申请入群是否需要群主或者群管理员审批，默认是false。注：如果允许了群成员邀请用户进群（allowinvites为true），那么就不需要群主或群管理员审批了
     * return false 或者true
     */
    public function createGroup($groupname, $desc, $public, $allowinvites, $membersonly = false, $owner)
    {
        if (empty($groupname) || empty($desc)   || empty($owner)) {

            return false;
        }
        $options = [
            'groupname' => $groupname,
            'desc' => $desc,
            'public' => $public,
            'allowinvites' => $allowinvites,
            'members_only' => $membersonly,
            'owner' => $owner,
        ];
        $url = $this->url . 'chatgroups';
        $body = json_encode($options);
        $header = array($this->getToken());
        $data = postCurl($url, $body, $header, 'POST');
        return resultHuanxinInfo($data);
    }

    /**
     * 修改群组信息
     * $groupname 群组名称，此属性为必须的，最大长度为128字符
     * $desc 群组描述，此属性为必须的，最大长度为512字符
     * $public 是否是公开群，此属性为必须的
     * $allowinvites 是否允许群成员邀请别人加入此群。 true：允许群成员邀请人加入此群，false：只有群主或者管理员才可以往群里加人。注：如果是公开群（public为true），则不允许群成员邀请别人加入此群
     *$membersonly 加入群组是否需要群主或者群管理员审批。true：是，false：否。
     * return false 或者true
     */
    public function updateGroup($groupname, $desc, $allowinvites, $membersonly = false, $public, $group_id)
    {
        if (empty($groupname) || empty($group_id)) {
            return false;
        }
        $options = [
            'groupname' => $groupname,
            'desc' => $desc,
            'public' => $public,
            'allowinvites' => $allowinvites,
            'membersonly' => $membersonly,
        ];
        $url = $this->url . 'chatgroups/' . $group_id;
        $body = json_encode($options);
        $header = array($this->getToken());
        $data = postCurl($url, $body, $header, 'PUT');
        return resultHuanxinInfo($data);
    }

    /**
     * 删除群组
     * $group_id 群组ID
     * return false 或者true
     */
    public function deleteGroup($group_id, $type = '')
    {
        if (empty($group_id)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data, $type);
    }

    /**
     * 分页获取群组成员
     * $group_id 群组ID
     * return false 或者true
     */
    public function getChatGroups($group_id)
    {
        if (empty($group_id)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/users';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 添加单个群组成员
     * $group_id 群组ID
     * $username用户环信ID
     * return false 或者true
     */
    public function addChatGroupsMember($group_id, $username)
    {
        if (empty($group_id) || empty($username)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/users/' . $username;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'POST');
        return resultHuanxinInfo($data);
    }

    /**
     * 群组批量加人成员
     * $group_id 群组ID
     * $usernames   用户环信ID    $usernames['usernames'] =  ['91','90'];
     * return false 或者true
     */
    public function addChatGroupsMembers($group_id, $usernames)
    {
        if (empty($group_id) || empty($usernames)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/users';
        $header = array($this->getToken());
        $body = json_encode($usernames);
        $data = postCurl($url, $body, $header, 'POST');
        return resultHuanxinInfo($data);
    }

    /**
     * 移除单个群组成员
     * $group_id 群组ID
     * $username用户环信ID
     * return false 或者true
     */
    public function delChatGroupsMember($group_id, $username)
    {
        if (empty($group_id) || empty($username)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/users/' . $username;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data);
    }

    /**
     * 批量移除单个群组成员
     * $group_id 群组ID
     * $usernames  用户环信ID    $usernames ='91,90';
     * return false 或者true
     */
    public function delChatGroupsMembers($group_id, $usernames)
    {
        if (empty($group_id) || empty($usernames)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/users/' . $usernames;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data);
    }

    /**
     * 获取群管理员列表
     * $group_id 群组ID
     * return false 或者true
     */
    public function getChatGroupsMemberAdmin($group_id)
    {
        if (empty($group_id)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/admin/';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 添加群管理员
     * $group_id 群组ID
     * $username用户环信ID
     * return false 或者true
     */
    public function addChatGroupsAdmin($group_id, $username)
    {
        if (empty($group_id) || empty($username)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/admin/';
        $header = array($this->getToken());
        $body = [
            'newadmin' => $username
        ];
        $body = json_encode($body);
        $data = postCurl($url, $body, $header, 'POST');
        return resultHuanxinInfo($data);
    }

    /**
     * 移除群管理员
     * $group_id 群组ID
     * $oldadmin 用户环信ID
     * return false 或者true
     */
    public function delChatGroupsAdmin($group_id, $oldadmin)
    {
        if (empty($group_id) || empty($oldadmin)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/admin/' . $oldadmin;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data);
    }

    /**
     * 转让群组
     * $group_id 群组ID
     * $newowner  用户环信ID
     * return false 或者true
     */
    public function transferChatGroupsAdmin($group_id, $newowner)
    {
        if (empty($group_id) || empty($newowner)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id;
        $header = array($this->getToken());
        $options = [
            'newowner' => $newowner
        ];
        $body = json_encode($options);
        $data = postCurl($url, $body, $header, 'PUT');
        return resultHuanxinInfo($data);
    }

    /**
     * 查询群组黑名单
     * $group_id 群组ID
     * return false 或者true
     */
    public function getGroupsBlacklist($group_id)
    {
        if (empty($group_id)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/blocks/users';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 添加单个用户至群组黑名单
     * $group_id 群组ID
     * $username 用户环信ID
     * return false 或者true
     */
    public function addGroupsBlacklist($group_id, $username)
    {
        if (empty($group_id) || empty($username)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/blocks/users/' . $username;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'POST');
        return resultHuanxinInfo($data);
    }

    /**
     * 群组黑名单批量加人
     * $group_id 群组ID
     * $usernames 用户环信ID  $usernames['usernames'] = ['91','90'];
     * return false 或者true
     */
    public function addGroupsBlacklists($group_id, $usernames)
    {
        if (empty($group_id) || empty($usernames)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/blocks/users';
        $header = array($this->getToken());
        $body = json_encode($usernames);
        $data = postCurl($url, $body, $header, 'POST');
        return resultHuanxinInfo($data);
    }

    /**
     * 从群组黑名单移除单个用户
     * $group_id 群组ID
     * $username 用户环信ID
     * return false 或者true
     */
    public function delGroupsBlacklist($group_id, $username)
    {
        if (empty($group_id) || empty($username)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/blocks/users/' . $username;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data);
    }

    /**
     * 群组黑名单批量减人
     * $group_id 群组ID
     * $usernames 用户环信ID  $usernames ='91,90';
     * return false 或者true
     */
    public function delGroupsBlacklists($group_id, $usernames)
    {
        if (empty($group_id) || empty($usernames)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/blocks/users/' . $usernames;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data);
    }

    /**
     * 批量/单个添加禁言
     * $group_id 群组ID
     * $username 用户环信ID   批量 $username =['91','90'];  单个  $username =['88'];
     *$mute_duration 禁言的时间，单位毫秒，如果是“-1”代表永久（实际的到期时间为固定时间戳4638873600000，即2117-01-01 00:00:00）
     * return false 或者true
     */
    public function addGroupsMute($group_id, $username, $mute_duration)
    {
        if (empty($group_id) || empty($username) || empty($mute_duration)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/mute';
        $header = array($this->getToken());
        $options = [
            'mute_duration' => $mute_duration,
            'usernames' => $username
        ];
        $body = json_encode($options, true);
        $data = postCurl($url, $body, $header, 'POST');
        return resultHuanxinInfo($data);
    }

    /**
     * 批量/单个 移除禁言
     * $group_id 群组ID
     * $username 用户环信ID   单个：$username ='91';  批量：$username ='90,88';
     * return false 或者true
     */
    public function delGroupsMute($group_id, $username)
    {
        if (empty($group_id) || empty($username)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/mute/' . $username;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data);
    }

    /**
     * 获取禁言列表
     * $group_id 群组ID
     * return false 或者true
     */
    public function getdelGroupsMute($group_id)
    {
        if (empty($group_id)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/mute';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 禁言全体成员
     * $group_id 群组ID
     * mute_duration 禁言时长
     * return false 或者true
     */
    public function GroupsBan($group_id,$mute_duration)
    {
        if (empty($group_id) || empty($mute_duration)) {
            return false;
        }
        $options = [
            'mute_duration' => $mute_duration,
        ];
        $body = json_encode($options, true);
        $url = $this->url . 'chatgroups/' . $group_id . '/ban';
        $header = array($this->getToken());
        $data = postCurl($url, $body, $header, 'POST');
        return resultHuanxinInfo($data);
    }

    /**
     * 解除全员禁言
     * $group_id 群组ID
     * return false 或者true
     */
    public function GroupsDelBan($group_id)
    {
        if (empty($group_id)) {
            return false;
        }
        $url = $this->url . 'chatgroups/' . $group_id . '/ban';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data);
    }

}
